//
//  ApplicitonClass.m
//  斗鸡TV
//
//  Created by admin on 16/9/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ApplicitonClass.h"

#import "AFNetworking.h"

@interface ApplicitonClass ()


@property(nonatomic,strong)  AFHTTPSessionManager * manager;
@end

@implementation ApplicitonClass

-(instancetype)init{
    
    self = [super init];
    
    self.manager = [AFHTTPSessionManager manager];
    
    return self;
}
+(instancetype)getInstance
{
    static ApplicitonClass * instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [ApplicitonClass new];
    });
    
    return instance;
}

- (UIInterfaceOrientationMask)interfaceOrientationMask {
    if (self.isflag == YES) {
        
        return UIInterfaceOrientationMaskLandscape;
        
    }else {
        
        return UIInterfaceOrientationMaskPortrait;
    }
}



//-(void)httpPOSTUrl:(NSString *)urlStr params:(NSDictionary *)params
//{
//    
//    //校验证书是否在信任列表中，然后再对比服务端证书和客户端证书是否一致
//    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
//    securityPolicy.allowInvalidCertificates = YES;      //是否允许使用自签名证书
//    securityPolicy.validatesDomainName = NO;
//    
//       AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
//    
//    
//    manager = [AFHTTPSessionManager manager];
//    [manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *_credential) {
//        
//        // 获取服务器的trust object
//        SecTrustRef serverTrust = [[challenge protectionSpace] serverTrust];
//        //导入自签名证书
//        NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"GlobalSign Organization Validation CA - SHA256 - G2" ofType:@"cer"];
//        NSData* caCert = [NSData dataWithContentsOfFile:cerPath];
//        NSArray *cerArray = [NSArray arrayWithObjects:caCert, nil];
//        NSSet * setArray = [NSSet setWithArray:cerArray];
//        manager.securityPolicy.pinnedCertificates = setArray;
//        
//        SecCertificateRef caRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);
//        NSCAssert(caRef != nil, @"caRef is nil");
//        
//        NSArray *caArray = @[(__bridge id)(caRef)];
//        NSCAssert(caArray != nil, @"caArray is nil");
//        
//        OSStatus status = SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)caArray);
//        SecTrustSetAnchorCertificatesOnly(serverTrust,NO);
//        NSCAssert(errSecSuccess == status, @"SecTrustSetAnchorCertificates failed");
//        //选择质询认证的处理方式
//        NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
//        __autoreleasing NSURLCredential *credential = nil;
//        
//        //NSURLAuthenticationMethodServerTrust质询认证方式
//        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
//            //基于客户端的安全策略来决定是否信任该服务器，不信任则不响应质询 。
//            if ([manager.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
//                //创建质询证书
//                credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
//                //确认质询方式
//                if (credential) {
//                    disposition = NSURLSessionAuthChallengeUseCredential;
//                } else {
//                    disposition = NSURLSessionAuthChallengePerformDefaultHandling;
//                }
//            } else {
//                //取消质询
//                disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
//            }
//        } else {
//            disposition = NSURLSessionAuthChallengePerformDefaultHandling;
//        }
//        
//        return disposition;
//    }];
//    
//    
// 
//    [manager POST:@"https://www.baidu.com" parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSDictionary * dict = (NSDictionary *)responseObject;
//        
//        if ([self.delegate respondsToSelector:@selector(successWithResponse:)]) {
//            
//            [self.delegate successWithResponse:dict];
//        }
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//          [self.delegate failedWithResponse:task Error:error];
//        
//    }];
//    
//}


-(void)httpGetUrl:(NSString *)urlStr params:(NSDictionary *)params
{
    
    [self.manager GET:urlStr parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * dict = (NSDictionary *)responseObject;
        
        if ([self.delegate respondsToSelector:@selector(successWithResponse:)]) {
            
            [self.delegate successWithResponse:dict];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.delegate failedWithResponse:task Error:error];
        
    }];
    
}

-(void)httpPOSTUrl:(NSString *)urlStr params:(NSDictionary *)params
{
    
    [self.manager POST:urlStr parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * dict = (NSDictionary *)responseObject;
        
        if ([self.delegate respondsToSelector:@selector(successWithResponse:)]) {
            
            [self.delegate successWithResponse:dict];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.delegate failedWithResponse:task Error:error];
        
    }];
    
}


@end
